import requests, bs4, csv
import pandas as pd
import matplotlib.pyplot as plt
import wordcloud
import imageio

csv_file=open('movieTop250.csv', 'w', newline='',encoding='utf-8')
writer = csv.writer(csv_file)
writer.writerow(['序号', '电影名', '评分', '推荐语', '演员','导演','年份','国家','类型'])

headers={'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'}
for x in range(10):
    url = 'https://movie.douban.com/top250?start=' + str(x*25) + '&filter='
    res = requests.get(url, headers=headers)
    bs = bs4.BeautifulSoup(res.text, 'html.parser')
    bs = bs.find('ol', class_="grid_view")
    for titles in bs.find_all('li'):
        num = titles.find('em',class_="").text
        title = titles.find('span', class_="title").text
        comment = titles.find('span',class_="rating_num").text
        others=titles.find('div',class_='bd').find('p').text.strip('').split('\n')

        if '\xa0\xa0\xa0' in others[1]:
            actors_director=others[1].strip('').split('\xa0\xa0\xa0')
            director=actors_director[0].strip(' ')
            actors=actors_director[1]
        else:
            actors_director=others[1].strip('').split('\xa0\xa0\xa0')
            director=actors_director[0].strip(' ')
            actors=''

        year_country_type=others[2].strip('').split('\xa0/\xa0')
        year=year_country_type[0].strip(' ')
        country=year_country_type[1]
        movie_type=year_country_type[2]

        if titles.find('span',class_="inq") != None:
            tes = titles.find('span',class_="inq").text
            writer.writerow([num , title , comment , tes , actors,director, year, country, movie_type])
        else:
            writer.writerow([num , title , comment , '', actors,director, year, country, movie_type])

csv_file.close()

#
movie_data = pd.read_csv('movieTop250.csv')
movie_data.head()
year = []
for i in movie_data["年份"]:
    i = i[0:4]
    year.append(i)
movie_data["年份"] = year
movie_data["年份"].value_counts()
x1 = list(movie_data["年份"].value_counts().sort_index().index)
y1 = list(movie_data["年份"].value_counts().sort_index().values)
y1 = [str(i) for i in y1]
year_counts = movie_data['年份'].value_counts()
year_counts.columns=['年份','次数']
plt.figure(figsize=(15, 8.5))
year_counts.sort_index().plot(kind='bar')
plt.show()

#
df=pd.read_csv("movieTop250.csv")
df.head(2)
df=df.drop(['序号','电影名','评分','推荐语','导演','年份','国家','类型'],axis=1)
df.to_csv("movie1.csv",index=False,encoding="utf-8")
import jieba
txt = open("movie1.csv","r",encoding='utf-8').read()
excludes={"演员","主演","..."}
words = jieba.lcut(txt)
counts = {}
for word in words:
    if len(word)==1:
        continue
    elif word == "Cheung" or word == "Leslie":
        rword = "张国荣"
    elif word == "Tom" or word == "tom":
        rword = "汤姆"
    elif word == "Hanks":
        rword = "汉克斯"
    elif word == "Stephen" or word == "Chow":
        rword = "周星驰"
    else:
        rword =word
        counts[word]=counts.get(word,0)+1
for word in excludes:
    del counts[word]
items = list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
for i in range(10):
    word, count = items[i]
    print ("{0:<10}{1:>5}".format(word, count))

#
df = pd.read_csv("movieTop250.csv")
df.head(2)
df = df.drop(['序号', '电影名', '评分', '推荐语', '导演', '年份', '国家', '演员'], axis=1)
df.to_csv("movie20.csv", index=False, encoding="utf-8")

mask = imageio.imread("fivestart.png")
excludes ={ }
f=open("movie20.csv","r",encoding='utf-8').read()
txt="".join(f)
w = wordcloud.WordCloud(\
                        width = 1000, height = 700,\
                        background_color = "white",
                        font_path = "msyh.ttc",mask = mask
                        )
w.generate(txt)
w.to_file("词云图.png")